import '../../model/UserEntity.dart';
import '../../utils/Tool/MyTools.dart';
import '../../utils/userbloc/user_bloc.dart';
import '../ApiUtils.dart';
import '../R.dart';

Future<R> google(String code, {int force = 0}) async {
  String url = "/api/v1.oauth/common/google";
  Map<String, dynamic> map = {};
  map['code'] = code;
  map['force'] = force;
  dynamic methodAwait = await ApiUtils.methodAwait(ApiUtils.postMethod, url,
      param: map, isShowLog: true);

  R r = R.fromData(methodAwait);

  return r;
}

// 模板识别码,对应管理后台“平台管理 => 邮件模板 => 识别码[
// MAIL_CODE_CAPTCHA:验证码,
// MAIL_CODE_REGISTER:注册账号验证码,
// MAIL_CODE_FINDPW:找回密码]”
Future<R> send(String recipient, String identify) async {
  String url = "/api/v1.mail/send";
  Map<String, dynamic> map = {};
  map['recipient'] = recipient;
  map['identify'] = identify;
  dynamic methodAwait = await ApiUtils.methodAwait(ApiUtils.postMethod, url,
      param: map, isShowLog: true);

  R r = R.fromData(methodAwait);

  return r;
}

Future<R> register({
  String? email,
  String? password,
  String? ssid,
  String? code,
}) async {
  String url = "api/v1.common/register";
  Map<String, dynamic> map = {};
  map['email'] = email;
  map['password'] = password;
  map['type'] = 5;
  if (!MyTools.stringIsEmpty(ssid)) {
    map['ssid'] = ssid;
  }
  if (!MyTools.stringIsEmpty(code)) {
    map['code'] = code;
  }

  print("register$map");
  dynamic methodAwait = await ApiUtils.methodAwait(ApiUtils.postMethod, url,
      param: map, isShowLog: true);

  R r = R.fromData(methodAwait);

  return r;
}

Future<R> login({
  String? email,
  String? password,
}) async {
  String url = "api/v1.common/login";
  Map<String, dynamic> map = {};
  map['type'] = 5;
  if (!MyTools.stringIsEmpty(email)) {
    map['email'] = email;
  }
  if (!MyTools.stringIsEmpty(password)) {
    map['password'] = password;
  }
  dynamic methodAwait = await ApiUtils.methodAwait(ApiUtils.postMethod, url,
      param: map, isShowLog: true);

  R r = R.fromData(methodAwait);

  return r;
}

Future<R> info() async {
  String url = "api/v1.user/info";
  Map<String, dynamic> map = {};
  dynamic methodAwait = await ApiUtils.methodAwait(ApiUtils.postMethod, url,
      param: map, isShowLog: true);
  R r = R.fromData(methodAwait, handlerData: (map) {
    UserEntity userEntity = UserEntity.fromJson(map);
    UserBlocTool.update(userEntity);
    return userEntity;
  });
  return r;
}

Future<R> findpw(
    {String? email, String? password, String? code, String? ssid}) async {
  String url = "api/v1.common/findpw";
  Map<String, dynamic> map = {};
  map['type'] = 1;
  map['content'] = email;
  map["ssid"] = ssid;
  map["passwd"] = password;
  map["code"] = code;
  dynamic methodAwait = await ApiUtils.methodAwait(ApiUtils.postMethod, url,
      param: map, isShowLog: true);
  R r = R.fromData(methodAwait);
  return r;
}

Future<R> accheck(String email) async {
  String url = "/api/v1.common/accheck";
  Map<String, dynamic> map = {};
  map['type'] = 5;
  map['email'] = email;
  dynamic methodAwait = await ApiUtils.methodAwait(ApiUtils.postMethod, url,
      param: map, isShowLog: true);
  R r = R.fromData(methodAwait);
  return r;
}
